﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.Revit;
using YRevitJig;

namespace YArchitech.HVAC
{
	internal class DimInHomeJig : XUiJigBase
	{
		public DimInHomeJig(UIDocument uDoc, InHomePipeData fD) : base(uDoc)
		{
			this.uiDoc = uDoc;
			this.fromData = fD;
		}

		public bool startPoint()
		{
			bool result;
			try
			{
				this.refPipe = this.uiDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)1, new PipeFilter(), "请选择入户管道");
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public bool endPoint()
		{
			bool result;
			try
			{
				this.PointEnd = this.uiDoc.Selection.PickPoint("请选取标注位置");
				result = true;
			}
			catch
			{
				result = false;
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			System.Drawing.Point ptCur = this.m_ptCur;
			if (this.m_ptCur.IsEmpty || paintEventArgs == null)
			{
				return;
			}
			if (this.refPipe == null)
			{
				return;
			}
			try
			{
				this.nRadius = this.GetRadio();
				XYZ globalPoint = this.refPipe.GlobalPoint;
				base.Client2Revit(this.m_ptCur);
				XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
				new XCircle2D(this.m_ptCur, this.nRadius).Draw(g);
				if (this.fromData != null && this.fromData.IsLead)
				{
					System.Drawing.Point ptStart = new System.Drawing.Point(this.m_ptCur.X, this.m_ptCur.Y);
					XYZ lineEndPoint = this.GetLineEndPoint(ref ptStart, this.refPipe);
					if (lineEndPoint != null)
					{
						new XLine2D(ptStart, base.Revit2Client(lineEndPoint)).Draw(g);
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw ex;
			}
		}

		private int GetRadio()
		{
			double num = 1.9685039370078738;
			num = num * (double)this.uiDoc.ActiveView.Scale / 100.0;
			XYZ xyz = base.Client2Revit(this.m_ptCur);
			XYZ point = new XYZ(xyz.X + num, xyz.Y, xyz.Z);
			System.Drawing.Point point2 = base.Revit2Client(point);
			return Convert.ToInt32(Math.Sqrt((double)(Math.Abs(this.m_ptCur.X - point2.X) * Math.Abs(this.m_ptCur.X - point2.X) + Math.Abs(this.m_ptCur.Y - point2.Y) * Math.Abs(this.m_ptCur.Y - point2.Y))));
		}

		private XYZ GetLineEndPoint(ref System.Drawing.Point xPipe, Reference refPipe)
		{
			XYZ result;
			try
			{
				if (refPipe == null)
				{
					result = null;
				}
				else
				{
					XYZ point = new XYZ(base.Client2Revit(xPipe).X, base.Client2Revit(xPipe).Y, 0.0);
					Curve curve = ((this.uiDoc.Document.GetElement(refPipe.ElementId) as Pipe).Location as LocationCurve).Curve;
					XYZ project = new YJKLine(new XYZ(curve.GetEndPoint(0).X, curve.GetEndPoint(0).Y, 0.0), new XYZ(curve.GetEndPoint(1).X, curve.GetEndPoint(1).Y, 0.0)).GetProject(point);
					if (project == null)
					{
						result = null;
					}
					else if (Math.Sqrt((double)(Math.Abs(xPipe.X - base.Revit2Client(project).X) * Math.Abs(xPipe.X - base.Revit2Client(project).X) + Math.Abs(xPipe.Y - base.Revit2Client(project).Y) * Math.Abs(xPipe.Y - base.Revit2Client(project).Y))) <= (double)this.nRadius)
					{
						result = null;
					}
					else
					{
						base.Client2Revit(new System.Drawing.Point(xPipe.X, xPipe.Y + this.nRadius));
						XYZ xyz = base.Client2Revit(new System.Drawing.Point(xPipe.X, xPipe.Y - this.nRadius));
						XYZ xyz2 = base.Client2Revit(new System.Drawing.Point(xPipe.X + this.nRadius, xPipe.Y));
						XYZ xyz3 = base.Client2Revit(new System.Drawing.Point(xPipe.X - this.nRadius, xPipe.Y));
						xPipe = new System.Drawing.Point(xPipe.X, xPipe.Y + this.nRadius);
						if (base.Client2Revit(xPipe).DistanceTo(project) > xyz.DistanceTo(project))
						{
							xPipe = new System.Drawing.Point(xPipe.X, xPipe.Y - this.nRadius);
						}
						if (base.Client2Revit(xPipe).DistanceTo(project) > xyz2.DistanceTo(project))
						{
							xPipe = new System.Drawing.Point(xPipe.X + this.nRadius, xPipe.Y);
						}
						if (base.Client2Revit(xPipe).DistanceTo(project) > xyz3.DistanceTo(project))
						{
							xPipe = new System.Drawing.Point(xPipe.X - this.nRadius, xPipe.Y);
						}
						result = project;
					}
				}
			}
			catch
			{
				result = null;
			}
			return result;
		}

		private int nRadius = 15;

		public XYZ PointEnd = XYZ.Zero;

		public Reference refPipe;

		private UIDocument uiDoc;

		private InHomePipeData fromData;
	}
}
